import axios from 'axios'
import { AJAX_SERVICE_HOST, RES_CODE_OK } from 'api/config'
import { wxLogin } from 'api/account'
import { getToken, updateUserVIP } from 'assets/js/cache'

axios.defaults.baseURL = AJAX_SERVICE_HOST

// 请求拦截器
axios.interceptors.request.use(
  config => {
    if (getToken()) {
      config.headers.common['X-Api-Authorization'] = getToken()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 返回拦截器
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

function checkCode(res, showToast, successCallback, failcalback) {
  const { code, msg } = res
  let returnVal = true
  if (code !== RES_CODE_OK) {
    returnVal = false
    switch (code) {
      case '10000':
        // error
        break
    }
    if (showToast) {
      Toast && Toast(msg)
    }
  }
  return returnVal
}

function checkNetwork(callFun) {
  callFun()
  // var counter = 3
  // function getNetworkStatus() {
  //   counter--
  //   var status = plus.networkinfo.getCurrentType()
  //   if (status > 1) {
  //     callFun()
  //   } else {
  //     if (counter === 0) {
  //       toastNetworkError()
  //     }
  //     setTimeout(getNetworkStatus, 1000)
  //   }
  // }
  // getNetworkStatus()
}

function toastNetworkError() {
  console.warn('网络不可用!')
  Toast && Toast('网络不可用!')
}

export default {
  get(url, params = {}, showToast = true) {
    return new Promise((resolve, reject) => {
      function _doFetch() {
        checkNetwork(() => {
          axios
            .get(url, {params: params})
            .then(
              response => {
                const res = response.data
                const checkCodeRes = checkCode(res, showToast, _doFetch, () => { reject(res) })
                switch (checkCodeRes) {
                  case true:
                    resolve(res.data)
                    break

                  case false:
                    try {
                      reject(res)
                    } catch (ex) {}
                    break
                }
              },
              err => {
                try {
                  toastNetworkError()
                  reject(err)
                } catch (ex) {}
              }
            )
            .catch(error => {
              toastNetworkError()
              reject(error)
            })
        })
      }
      _doFetch()
    })
  },
  post(url, params = {}, showToast = true) {
    return new Promise((resolve, reject) => {
      function _doFetch() {
        checkNetwork(() => {
          axios
            .post(url, params)
            .then(
              response => {
                const res = response.data
                const checkCodeRes = checkCode(res, showToast, _doFetch, () => { reject(res) })
                switch (checkCodeRes) {
                  case true:
                    resolve(res.data)
                    break

                  case false:
                    try {
                      reject(res)
                    } catch (ex) {}
                    break
                }
              },
              err => {
                try {
                  toastNetworkError()
                  reject(err)
                } catch (ex) {}
              }
            )
            .catch(error => {
              toastNetworkError()
              reject(error)
            })
        })
      }
      _doFetch()
    })
  }
}
